<template>
  <div class="org-tree-search">
    <div class="org-tree-search-head">
      <el-input
        v-model="searchValue"
        placeholder="搜索"
        prefix-icon="el-icon-search"
        @input="onSearchChange"
      ></el-input>
    </div>
    <div v-if="isSearch" class="org-tree-search-cont">
      <div v-if="isEmpty" class="org-tree-search-empty">
        <div class="org-tree-search-empty-img-box">
          <img src="@/assets/img/org-search-empty.png" alt="" />
        </div>
        <div class="org-tree-search-empty-text">抱歉，没有找到“{{ searchValue }}”的搜索结果</div>
      </div>
      <div class="org-tree-search-list">
        <div
          v-for="item in searchList"
          :key="item.id"
          class="org-tree-search-item"
          :class="{ 'is-curr': item.isCurr }"
        >
          <el-checkbox
            v-if="showCheckbox"
            v-model="item.checked"
            class="org-tree-search-item-checkbox"
            @change="onSearchItemClick(item)"
          >
            {{ item.deptName }}
          </el-checkbox>
          <div v-else class="org-tree-search-item-text" @click="onSearchItemClick(item)">
            {{ item.deptName }}
          </div>
        </div>
      </div>
    </div>
    <div v-else class="org-tree-search-cont">
      <el-tree
        ref="tree"
        node-key="id"
        accordion
        check-strictly
        :data="orgTree"
        :expand-on-click-node="false"
        :default-expanded-keys="expandedKeys"
        :show-checkbox="showCheckbox"
        :props="{
          label: 'deptName',
          children: 'organizationTreeVo',
        }"
        @current-change="onCurrentChange"
        @check="onCheck"
      ></el-tree>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex';
  import { treeTransArray } from '@/utils/treeUtil';
  import { deepClone } from '@/utils/base';

  export default {
    props: {
      showCheckbox: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        isSearch: false,
        isEmpty: false,
        searchValue: '',
        searchList: [],
        selectList: [],
      };
    },
    computed: {
      ...mapGetters('organization', ['orgTree']),
      expandedKeys() {
        return this.orgTree.length > 0 ? [this.orgTree[0].id] : [];
      },
      tranArr() {
        return treeTransArray(this.orgTree, 'organizationTreeVo').map(item => ({
          ...item,
          isCurr: false,
        }));
      },
    },
    methods: {
      init() {
        this.isSearch = false;
        this.isEmpty = false;
        this.searchList = [];
        this.searchValue = '';
      },
      onCurrentChange(data) {
        if (!this.showCheckbox) {
          // eslint-disable-next-line no-unused-vars
          const { organizationTreeVo, ...item } = data;
          this.$emit('on-select-change', item);
        }
      },
      onCheck(node, { checkedNodes }) {
        if (this.showCheckbox) {
          this.selectList = checkedNodes;
          this.$emit('on-select-change', checkedNodes);
        }
      },
      onSearchChange(value) {
        this.isSearch = value !== '';
        this.searchList = deepClone(
          this.tranArr.filter(item => {
            return item.deptName.includes(value);
          })
        );
        this.isEmpty = this.searchList.length <= 0;
        if (!this.isSearch) {
          this.$nextTick(() => {
            this.$refs['tree'].setCheckedNodes(this.selectList);
          });
        }
      },
      onSearchItemClick(item) {
        if (this.showCheckbox) {
          const ids = this.selectList.map(m => m.id);
          this.selectList = this.tranArr.filter(m => {
            if (m.id === item.id) {
              return !!item.checked;
            }
            return ids.includes(m.id);
          });
          this.$emit('on-select-change', deepClone(this.selectList));
        } else {
          this.searchList.forEach(m => {
            m.isCurr = item.id === m.id;
          });
          this.$emit('on-select-change', { ...item });
        }
      },
      unselectItem(item) {
        this.selectList = this.selectList.filter(m => m.id !== item.id);
        if (this.isSearch) {
          this.searchList.forEach(m => {
            if (m.id === item.id) {
              m.checked = false;
            }
          });
        } else {
          this.$refs['tree'].setCheckedNodes(this.selectList);
        }
        this.$emit('on-select-change', deepClone(this.selectList));
      },
    },
  };
</script>
<style lang="scss" scoped>
  .org-tree-search {
    height: 338px;
  }
  .org-tree-search-head {
    height: 38px;
  }
  .org-tree-search-cont {
    height: 300px;
    overflow-y: auto;
    overflow-x: hidden;
    margin-top: 20px;
  }
  .org-tree-search-modal-title {
    color: #b4b8be;
  }
  .org-tree-search-empty {
    text-align: center;
  }
  .org-tree-search-item {
    line-height: 34px;
    cursor: pointer;
    padding: 0 15px;
    &:hover,
    &.is-curr {
      background-color: #f0f7ff;
    }
  }
  .org-tree-search-item-text {
    height: 34px;
    line-height: 34px;
  }
  .org-tree-search-item-checkbox {
    width: 100%;
  }
</style>
<style lang="scss">
  .org-tree-search-item-checkbox {
    .el-checkbox__label {
      color: unset !important;
    }
  }
</style>
